package com.example.administrator.szb.activity.teseservice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.JGXQActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.common.CommonPost;
import com.example.administrator.szb.common.ReslutBean;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.SeriveList;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.ShareUtils;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;
import java.util.Map;

public class TeseDeatils extends MVPBaseActivity {
    private TextView tv_fa_time;
    private TextView tv_time;
    private LinearLayout ll_go_ucenter;
    private LinearLayout look_more_ll;
    private com.makeramen.roundedimageview.RoundedImageView img2;
    private TextView name2;
    private TextView gwgy;
    private CheckBox jgxq_check_sc;
    private LinearLayout ll_contacts,ll_bottoms;
    private TextView bar_title,title,tv_content,tv_bottom;
    private ImageView right_sc;
    private ImageView right_img;
    private int id=0;
    private SeriveList seriveList;
    private View ll_statusa;
    private TextView project_status;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tese_deatils);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        ll_statusa=findViewById(R.id.ll_statusa);
        project_status=findViewById(R.id.project_status);
        tv_bottom = findViewById(R.id.tv_bottom);
        ll_bottoms = findViewById(R.id.ll_bottoms);
        id=getIntent().getIntExtra("id",0);
        title = findViewById(R.id.title);
        tv_content = findViewById(R.id.tv_content);
        bar_title = (TextView) findViewById(R.id.bar_title);
        bar_title.setText("服务详情");
        right_sc = (ImageView) findViewById(R.id.right_sc);
        right_sc.setVisibility(View.GONE);
        right_img = (ImageView) findViewById(R.id.right_img);
        tv_fa_time = (TextView) findViewById(R.id.tv_fa_time);
        tv_time = (TextView) findViewById(R.id.tv_time);
        ll_go_ucenter = (LinearLayout) findViewById(R.id.ll_go_ucenter);
        look_more_ll = (LinearLayout) findViewById(R.id.look_more_ll);
        img2 = (com.makeramen.roundedimageview.RoundedImageView) findViewById(R.id.img2);
        name2 = (TextView) findViewById(R.id.name2);
        gwgy = (TextView) findViewById(R.id.gwgy);
        jgxq_check_sc = (CheckBox) findViewById(R.id.jgxq_check_sc);
        ll_contacts = (LinearLayout) findViewById(R.id.ll_contacts);
    }

    @Override
    protected void initEvent() {
        right_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CheckInfoUtils.isLogin(context)){
                    right_sc.setEnabled(false);
                    CommonPost.doSc(activity, 2, id, new CommonPost.OnShouCangListener() {
                        @Override
                        public void onSuccess(ReslutBean bean) {
                            right_sc.setEnabled(true);
                            Toasts.show(context,bean.getStr());
                            if (bean.getCode()==0){
                                right_sc.setImageResource(R.mipmap.sc_white_no);
                            }else {
                                right_sc.setImageResource(R.mipmap.sc_white_yes);

                            }
                        }

                        @Override
                        public void onFail(String str) {
                            right_sc.setEnabled(true);
                        }
                    });
                }
            }
        });
        ll_contacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckInfoUtils.check(activity, iosDiaolog, ll_contacts, new CheckInfoUtils.OnSuccessLietener() {
                    @Override
                    public void onSuccess() {
                        if (seriveList != null&&seriveList.getKefutel()!=null && seriveList.getKefutel().length() > 0) {
                            CommonPost.callPhone(activity,id,1);
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:" + seriveList.getKefutel());
                            intent.setData(data);
                            startActivity(intent);
                        }
                    }
                });
            }
        });
        look_more_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (seriveList==null){
                    return;
                }
                Intent intent = new Intent(context, JGXQActivity.class);
                intent.putExtra("id", seriveList.getUser_id());
                startActivity(intent);
            }
        });
        right_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //分享
                if (seriveList==null){
                    return;
                }
                ShareUtils.share(TeseDeatils.this,null,URLAddress.SERVICE_URL+id+"&app=1",seriveList.getTitle(),seriveList.getContent());
            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (seriveList==null){
                    return;
                }
                Intent intent = new Intent(context, JGXQActivity.class);
                intent.putExtra("id", seriveList.getUser_id());
                startActivity(intent);
            }
        });
        jgxq_check_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //DialogUtil.showIsoProgressbar(context,"正在加载");
                if (!CheckInfoUtils.isLogin(context)){
                    jgxq_check_sc.setEnabled(true);
                    jgxq_check_sc.setChecked(false);
                    return;
                }
                jgxq_check_sc.setEnabled(false);
                CommonPost.doSc(activity, 2, id, new CommonPost.OnShouCangListener() {
                    @Override
                    public void onSuccess(ReslutBean bean) {

                        //DialogUtil.dialog_ios.dismiss();
                        jgxq_check_sc.setEnabled(true);
                        Toasts.show(context,bean.getStr());
                        if (bean.getCode()==0){
                            jgxq_check_sc.setChecked(false);
                        }else {
                            jgxq_check_sc.setChecked(true);
                        }
                    }

                    @Override
                    public void onFail(String str) {

                       // DialogUtil.dialog_ios.dismiss();
                        jgxq_check_sc.setEnabled(true);
                        if (jgxq_check_sc.isChecked()){
                            jgxq_check_sc.setChecked(false);
                        }else {
                            jgxq_check_sc.setChecked(true);
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
        getInfo();
    }
    private void getInfo(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("uid", SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.SPECIAL_GETDEATIL, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                seriveList = JSON.parseObject(str,new TypeReference<SeriveList>(){});
                if (seriveList.getUser_id()==SPUtils.getUserId()){
                    ll_bottoms.setVisibility(View.GONE);
                }
                if (seriveList.getStatus()==3||seriveList.getStatus()==4){
                    ll_bottoms.setVisibility(View.GONE);
                    tv_bottom.setVisibility(View.VISIBLE);
                }

                if (seriveList.getStatus()==2){
                    ll_statusa.setVisibility(View.VISIBLE);
                    project_status.setText(seriveList.getMark());

                }
                title.setText(seriveList.getTitle());
                tv_fa_time.setText(seriveList.getCreate_time());
                tv_time.setText(seriveList.getEnd_time());
                tv_content.setText(seriveList.getContent());
                Glide.with(context).load(URLAddress.HOST_PORT+seriveList.getHeadimg()).error(R.mipmap.default_header_s).into(img2);
                name2.setText(seriveList.getUser_name());
                gwgy.setText(seriveList.getCity());
                if (seriveList.getIs_collect()==1){
                    jgxq_check_sc.setChecked(true);
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
}
